package com.example.javaee.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.javaee.common.result;
import com.example.javaee.entity.Asset;
import com.example.javaee.entity.User;
import com.example.javaee.service.AssetOrgService;
import com.example.javaee.service.AssetService;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/asset")
public class AssetController {
    @Autowired
    private AssetService assetService;

    //新增安全资产
    @PostMapping("/add")
    public result<Asset> add(HttpServletRequest request, @RequestBody Asset asset) {
        //获取属性
        String assetNo = asset.getAssetNo();
        String assetTypeNo = asset.getAssetTypeNo();
        String assetName = asset.getAssetName();
        int BelongOrg = asset.getBelongOrg();
        //根据页面提交的资产编号AssetNo查询数据库
        LambdaQueryWrapper<Asset> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Asset::getAssetNo, asset.getAssetNo());
        Asset as = assetService.getOne(queryWrapper);
        if (assetService.getOne(queryWrapper) != null) {
            return result.error("资产已存在");
        }
        assetService.save(asset);
        return result.success(null, "资产添加成功");
    }
    //通过id获取资产信息
    @GetMapping("/getAsset/{id}")
    public result<Asset> getAsset(@PathVariable Integer id){
        LambdaQueryWrapper<Asset> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Asset::getId, id);
        Asset as = assetService.getOne(queryWrapper);
        System.out.printf(String.valueOf(as));
        return result.success(as,"查询成功");
    }
     //通过id删除资产
    @DeleteMapping("/delete/{id}")
    public result<Asset> delete(@PathVariable Integer id) {
        if(assetService.removeById(id)){
            return  result.success(null,"删除成功");}
        else{
            return result.error("删除失败");
        }
    }
    //编辑安全资产
    @PostMapping("/update")
    public result<Asset> update(@RequestBody(required = false) Asset asset) {
        UpdateWrapper<Asset> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", asset.getId());
        if(assetService.update(asset, updateWrapper)){
            return result.success(null, "更新成功");
        } else {
            return result.error("更新失败");
        }
    }
    //  按照类别资产查询
    @GetMapping("/queryByCategory/{input}/{pg}")
    public result<Page> categoryPage(@PathVariable Integer pg, @PathVariable String input) {
        // 分页构造器
        Page<Asset> pageInfo = new Page<>(pg, 12);
        // 条件构造器
        LambdaQueryWrapper<Asset> queryWrapper = new LambdaQueryWrapper<>();
        // 如果有输入条件，则加入对name字段的筛选条件
        if (input != null && !input.isEmpty()) {
            queryWrapper.eq(Asset::getAssetCategory, input);
        }
        else {
            return result.error("请输入搜索内容");
        }
        // 分页查询
        assetService.page(pageInfo, queryWrapper);
        System.out.printf(String.valueOf(pageInfo));
        return result.success(pageInfo, "查询成功");
    }

    //按照标签搜索
    @GetMapping("/queryByLabel/{input}/{pg}")
    public result<Page> labelPage(@PathVariable Integer pg, @PathVariable String input) {
        // 分页构造器
        Page<Asset> pageInfo = new Page<>(pg, 12);
        // 条件构造器
        LambdaQueryWrapper<Asset> queryWrapper = new LambdaQueryWrapper<>();
        // 如果有输入条件，则加入对name字段的筛选条件
        if (input != null && !input.isEmpty()) {
            queryWrapper.eq(Asset::getAssetLabel, input);
        }
        else {
            return result.error("请输入搜索内容");
        }
        // 分页查询
        assetService.page(pageInfo, queryWrapper);
        System.out.printf(String.valueOf(pageInfo));
        return result.success(pageInfo, "查询成功");
    }
    //通过名字查询
    @GetMapping("/queryByName/{input}/{pg}")
    public result<Page> namePage(@PathVariable Integer pg, @PathVariable String input) {
        // 分页构造器
        Page<Asset> pageInfo = new Page<>(pg, 12);
        // 条件构造器
        LambdaQueryWrapper<Asset> queryWrapper = new LambdaQueryWrapper<>();
        // 如果有输入条件，则加入对name字段的筛选条件
        if (input != null && !input.isEmpty()) {
            queryWrapper.eq(Asset::getAssetName, input);
        }
        else {
            return result.error("请输入搜索内容");
        }
        // 分页查询
        assetService.page(pageInfo, queryWrapper);
        System.out.printf(String.valueOf(pageInfo));
        return result.success(pageInfo, "查询成功");
    }
    //通过组织查询
    @GetMapping("/queryByOrg/{input}/{pg}")
    public result<Page> orgPage(@PathVariable Integer pg, @PathVariable String input) {
        // 分页构造器
        Page<Asset> pageInfo = new Page<>(pg, 12);
        // 条件构造器
        LambdaQueryWrapper<Asset> queryWrapper = new LambdaQueryWrapper<>();
        // 如果有输入条件，则加入对name字段的筛选条件
        if (input != null && !input.isEmpty()) {
            queryWrapper.eq(Asset::getBelongOrg, input);
        }
        else {
            return result.error("请输入搜索内容");
        }
        // 分页查询
        assetService.page(pageInfo, queryWrapper);
        System.out.printf(String.valueOf(pageInfo));
        return result.success(pageInfo, "查询成功");
    }
    @GetMapping("/getAssets/{pg}")
    public result<Page> page(@PathVariable Integer pg){
        //分页构造器
            Page<Asset> pageInfo = new Page<>(pg,12);
        //条件构造器
        LambdaQueryWrapper<Asset> queryWrapper = new LambdaQueryWrapper<>();
        //分页查询
        assetService.page(pageInfo,queryWrapper);
        System.out.printf(String.valueOf(pageInfo));
        return result.success(pageInfo,"查询成功");

    }
}